#include <stdio.h>
#include <string.h>

void reverse(char * dst, const char * src)
{
    strcpy(dst, src);
}

void reverse2(char *s)
{
    strcpy(s, "hello reverse.");
}

int sum(int a, int b)
{
    a += b;
    return a;
}

int main()
{
    char s0[] = "Hello, asm!";
    char s1[100];

    reverse(&s1[0], &s0[0]);
    printf("%s\n", &s1[0]);

    reverse2(&s1[0]);
    printf("%s\n", &s1[0]);

    printf("%x\n", sum(0x666, 0x777));

	return 0;
}

/*
	Hello, asm!
	hello reverse.
	ddd
 */

#if 0

/*
 * intel
 */
00000000000011a9 <reverse>:
    11a9:	f3 0f 1e fa          	endbr64 
    11ad:	55                   	push   %rbp
    11ae:	48 89 e5             	mov    %rsp,%rbp
    11b1:	48 83 ec 10          	sub    $0x10,%rsp		// rsp-=0x10
    11b5:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)		// rbp-0x8=rdi
    11b9:	48 89 75 f0          	mov    %rsi,-0x10(%rbp)		// rbp-0x10=rsi
    11bd:	48 8b 55 f0          	mov    -0x10(%rbp),%rdx		// rdx=rbp-0x10
    11c1:	48 8b 45 f8          	mov    -0x8(%rbp),%rax		// rax=rbp-0x8
    11c5:	48 89 d6             	mov    %rdx,%rsi		// rsi=rdx
    11c8:	48 89 c7             	mov    %rax,%rdi		// rdi=rax
    11cb:	e8 b0 fe ff ff       	callq  1080 <strcpy@plt>	// strcpy(rdi, rsi)
    11d0:	90                   	nop
    11d1:	c9                   	leaveq 
    11d2:	c3                   	retq   

00000000000011d3 <reverse2>:
    11d3:	f3 0f 1e fa          	endbr64 
    11d7:	55                   	push   %rbp
    11d8:	48 89 e5             	mov    %rsp,%rbp
    11db:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)		// rbp-0x8=rdi
    11df:	48 8b 45 f8          	mov    -0x8(%rbp),%rax		// rax=rbp-0x8
    11e3:	48 ba 68 65 6c 6c 6f 	movabs $0x6572206f6c6c6568,%rdx	// rdx=0x6572206f6c6c6568
    11ea:	20 72 65 
    11ed:	48 89 10             	mov    %rdx,(%rax)		// (rax)=(rbp-0x8)=rdx
    11f0:	c7 40 08 76 65 72 73 	movl   $0x73726576,0x8(%rax)	// (rax+0x8)=(rbp-0x8+0x8)=0x73726576
    11f7:	66 c7 40 0c 65 2e    	movw   $0x2e65,0xc(%rax)	// (rax+0xc)=0x2e65
    11fd:	c6 40 0e 00          	movb   $0x0,0xe(%rax)		// (rax+0xe)=0x0
    1201:	90                   	nop
    1202:	5d                   	pop    %rbp
    1203:	c3                   	retq   

0000000000001204 <sum>:
    1204:	f3 0f 1e fa          	endbr64 
    1208:	55                   	push   %rbp
    1209:	48 89 e5             	mov    %rsp,%rbp		// rbp=rsp
    120c:	89 7d fc             	mov    %edi,-0x4(%rbp)		// (rbp-0x4)=edi
    120f:	89 75 f8             	mov    %esi,-0x8(%rbp)		// (rbp-0x8)=esi
    1212:	8b 45 f8             	mov    -0x8(%rbp),%eax		// eax=(rbp-0x8)
    1215:	01 45 fc             	add    %eax,-0x4(%rbp)		// (rbp-0x4)+=eax
    1218:	8b 45 fc             	mov    -0x4(%rbp),%eax		// eax=(rbp-0x4)
    121b:	5d                   	pop    %rbp
    121c:	c3                   	retq   

000000000000121d <main>:
    121d:	f3 0f 1e fa          	endbr64 
    1221:	55                   	push   %rbp
    1222:	48 89 e5             	mov    %rsp,%rbp		// rbp=rsp
    1225:	48 83 c4 80          	add    $0xffffffffffffff80,%rsp	// rsp&=0xffffffffffffff80
    1229:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax		// rax=fs:0x28
    1230:	00 00 
    1232:	48 89 45 f8          	mov    %rax,-0x8(%rbp)		// rbp-0x8=rax
    1236:	31 c0                	xor    %eax,%eax		// eax=0
    1238:	48 b8 48 65 6c 6c 6f 	movabs $0x61202c6f6c6c6548,%rax	// rax=0x61202c6f6c6c6548
    123f:	2c 20 61 
    1242:	48 89 45 84          	mov    %rax,-0x7c(%rbp)		// rbp-0x7c=rax
    1246:	c7 45 8c 73 6d 21 00 	movl   $0x216d73,-0x74(%rbp)	// rbp-0x74=0x216d73
    124d:	48 8d 55 84          	lea    -0x7c(%rbp),%rdx		// rdx=0x61202c6f6c6c6548
    1251:	48 8d 45 90          	lea    -0x70(%rbp),%rax		// rax=rbp-0x70
    1255:	48 89 d6             	mov    %rdx,%rsi		// rsi=rdx
    1258:	48 89 c7             	mov    %rax,%rdi		// rdi=rax
    125b:	e8 49 ff ff ff       	callq  11a9 <reverse>		// reverse(rdi, rsi)
    1260:	48 8d 45 90          	lea    -0x70(%rbp),%rax		// rax=rbp-0x70
    1264:	48 89 c7             	mov    %rax,%rdi		// rdi=rax
    1267:	e8 24 fe ff ff       	callq  1090 <puts@plt>		// puts(rdi)
    126c:	48 8d 45 90          	lea    -0x70(%rbp),%rax		// rax=rbp-0x70
    1270:	48 89 c7             	mov    %rax,%rdi		// rdi=rax
    1273:	e8 5b ff ff ff       	callq  11d3 <reverse2>		// revers2(rdi)
    1278:	48 8d 45 90          	lea    -0x70(%rbp),%rax		// rax=rbp-0x70
    127c:	48 89 c7             	mov    %rax,%rdi		// rdi=rax
    127f:	e8 0c fe ff ff       	callq  1090 <puts@plt>		// puts(rdi)
    1284:	be 77 07 00 00       	mov    $0x777,%esi		// esi=0x777
    1289:	bf 66 06 00 00       	mov    $0x666,%edi		// edi=0x666
    128e:	e8 71 ff ff ff       	callq  1204 <sum>		// sum(edi, esi)
    1293:	89 c6                	mov    %eax,%esi		// esi=eax
    1295:	48 8d 3d 68 0d 00 00 	lea    0xd68(%rip),%rdi        # 2004 <_IO_stdin_used+0x4>	rdi=rip+0xd68
    129c:	b8 00 00 00 00       	mov    $0x0,%eax		// eax=0
    12a1:	e8 0a fe ff ff       	callq  10b0 <printf@plt>	// printf(rdi, esi)
    12a6:	b8 00 00 00 00       	mov    $0x0,%eax		// eax=0
    12ab:	48 8b 4d f8          	mov    -0x8(%rbp),%rcx		// rcx=rbp-0x8
    12af:	64 48 33 0c 25 28 00 	xor    %fs:0x28,%rcx		// rcs=fs:0x28
    12b6:	00 00 
    12b8:	74 05                	je     12bf <main+0xa2>
    12ba:	e8 e1 fd ff ff       	callq  10a0 <__stack_chk_fail@plt>
    12bf:	c9                   	leaveq 
    12c0:	c3                   	retq   
    12c1:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
    12c8:	00 00 00 
    12cb:	0f 1f 44 00 00       	nopl   0x0(%rax,%rax,1)

/*
 * arm
 */
00000000004005fc <reverse>:
  4005fc:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400600:	910003fd 	mov	x29, sp
  400604:	f9000fa0 	str	x0, [x29, #24]		// x29+24=x0
  400608:	f9000ba1 	str	x1, [x29, #16]		// x29+16=x1
  40060c:	f9400ba1 	ldr	x1, [x29, #16]		// x1=x29+16
  400610:	f9400fa0 	ldr	x0, [x29, #24]		// x0=x29+24
  400614:	97ffffb3 	bl	4004e0 <strcpy@plt>	// strcpy(x0, x1)
  400618:	d503201f 	nop
  40061c:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400620:	d65f03c0 	ret

0000000000400624 <reverse2>:
  400624:	d10043ff 	sub	sp, sp, #0x10
  400628:	f90007e0 	str	x0, [sp, #8]		// sp+8=x0
  40062c:	f94007e2 	ldr	x2, [sp, #8]		// x2=sp+8
  400630:	90000000 	adrp	x0, 400000 <_init-0x460>
  400634:	911e8001 	add	x1, x0, #0x7a0		// x1=4007a0
  400638:	aa0203e0 	mov	x0, x2			// x0=sp+8
  40063c:	f9400022 	ldr	x2, [x1]		// x2=[4007a0]
  400640:	f9000002 	str	x2, [x0]		// [sp+8]=x2
  400644:	f8407021 	ldur	x1, [x1, #7]		// x1=[x1+7]
  400648:	f8007001 	stur	x1, [x0, #7]		// [x0+7]=x1
  40064c:	d503201f 	nop
  400650:	910043ff 	add	sp, sp, #0x10
  400654:	d65f03c0 	ret

0000000000400658 <sum>:
  400658:	d10043ff 	sub	sp, sp, #0x10
  40065c:	b9000fe0 	str	w0, [sp, #12]		// [sp+12]=w0
  400660:	b9000be1 	str	w1, [sp, #8]		// [sp+8]=w1
  400664:	b9400fe1 	ldr	w1, [sp, #12]		// w1=[sp+12]
  400668:	b9400be0 	ldr	w0, [sp, #8]		// w0=[sp+8]
  40066c:	0b000020 	add	w0, w1, w0		// w0=w1+w0
  400670:	b9000fe0 	str	w0, [sp, #12]		// [sp+12]=w0
  400674:	b9400fe0 	ldr	w0, [sp, #12]		// w0=[sp+12]
  400678:	910043ff 	add	sp, sp, #0x10		// sp=sp+0x10
  40067c:	d65f03c0 	ret

0000000000400680 <main>:
  400680:	a9b77bfd 	stp	x29, x30, [sp, #-144]!
  400684:	910003fd 	mov	x29, sp
  400688:	90000000 	adrp	x0, 400000 <_init-0x460>
  40068c:	911ee001 	add	x1, x0, #0x7b8		// x1=4007b8
  400690:	910203a0 	add	x0, x29, #0x80		// x0=x29+0x80
  400694:	f9400022 	ldr	x2, [x1]		// x2=[x1]
  400698:	f9000002 	str	x2, [x0]		// [x0]=x2
  40069c:	b9400821 	ldr	w1, [x1, #8]		// w1=[x1+8]
  4006a0:	b9000801 	str	w1, [x0, #8]		// [x0+8]=w1
  4006a4:	910203a1 	add	x1, x29, #0x80		// x1=x29+0x80
  4006a8:	910063a0 	add	x0, x29, #0x18		// x0=x29+0x18
  4006ac:	97ffffd4 	bl	4005fc <reverse>	// reverse(x0, x1)
  4006b0:	910063a0 	add	x0, x29, #0x18		// x0=x29+0x18
  4006b4:	97ffff87 	bl	4004d0 <puts@plt>	// puts(x0)
  4006b8:	910063a0 	add	x0, x29, #0x18		// x0=0x29+0x18
  4006bc:	97ffffda 	bl	400624 <reverse2>	// reverse2(x0)
  4006c0:	910063a0 	add	x0, x29, #0x18		// x0=x29+0x18
  4006c4:	97ffff83 	bl	4004d0 <puts@plt>	// puts(x0)
  4006c8:	5280eee1 	mov	w1, #0x777                 	// #1911
  4006cc:	5280ccc0 	mov	w0, #0x666                 	// #1638
  4006d0:	97ffffe2 	bl	400658 <sum>		// sum(w0, w1)
  4006d4:	2a0003e1 	mov	w1, w0			// w1=w0
  4006d8:	90000000 	adrp	x0, 400000 <_init-0x460>
  4006dc:	911ec000 	add	x0, x0, #0x7b0		// x0=4007b0
  4006e0:	97ffff84 	bl	4004f0 <printf@plt>	// printf(x0, w1)
  4006e4:	52800000 	mov	w0, #0x0                   	// #0
  4006e8:	a8c97bfd 	ldp	x29, x30, [sp], #144
  4006ec:	d65f03c0 	ret

/*
 * arm-v7
 */
void reverse(char * dst, const char * src)
{
   1051c:	e92d4800 	push	{fp, lr}
   10520:	e28db004 	add	fp, sp, #4
   10524:	e24dd008 	sub	sp, sp, #8
   10528:	e50b0008 	str	r0, [fp, #-8] ; dst
   1052c:	e50b100c 	str	r1, [fp, #-12] ; src
    strcpy(dst, src);
   10530:	e51b100c 	ldr	r1, [fp, #-12] ; src
   10534:	e51b0008 	ldr	r0, [fp, #-8]  ; dst
   10538:	ebffff8e 	bl	10378 <strcpy@plt>
}
   1053c:	e24bd004 	sub	sp, fp, #4
   10540:	e8bd8800 	pop	{fp, pc}

00010544 <reverse2>:

void reverse2(char *s)
{
   10544:	e92d4800 	push	{fp, lr}
   10548:	e28db004 	add	fp, sp, #4
   1054c:	e24dd008 	sub	sp, sp, #8
   10550:	e50b0008 	str	r0, [fp, #-8] ; s
    strcpy(s, "hello reverse.");    
   10554:	e51b3008 	ldr	r3, [fp, #-8] ; s
   10558:	e59f2018 	ldr	r2, [pc, #24]	; 10578 <reverse2+0x34>
   1055c:	e1a00003 	mov	r0, r3
   10560:	e1a01002 	mov	r1, r2
   10564:	e3a0300f 	mov	r3, #15
   10568:	e1a02003 	mov	r2, r3
   1056c:	ebffff87 	bl	10390 <memcpy@plt>
}
   10570:	e24bd004 	sub	sp, fp, #4
   10574:	e8bd8800 	pop	{fp, pc}
   10578:	0001064c 	.word	0x0001064c

0001057c <sum>:

int sum(int a, int b)
{
   1057c:	e52db004 	push	{fp}		; (str fp, [sp, #-4]!)
   10580:	e28db000 	add	fp, sp, #0
   10584:	e24dd00c 	sub	sp, sp, #12
   10588:	e50b0008 	str	r0, [fp, #-8] ; a
   1058c:	e50b100c 	str	r1, [fp, #-12] ; b
    a += b;
   10590:	e51b2008 	ldr	r2, [fp, #-8] ; a
   10594:	e51b300c 	ldr	r3, [fp, #-12] ; b
   10598:	e0823003 	add	r3, r2, r3     ; a+b
   1059c:	e50b3008 	str	r3, [fp, #-8]  ; a
    return a;
   105a0:	e51b3008 	ldr	r3, [fp, #-8] ; a
}
   105a4:	e1a00003 	mov	r0, r3
   105a8:	e24bd000 	sub	sp, fp, #0
   105ac:	e49db004 	pop	{fp}		; (ldr fp, [sp], #4)
   105b0:	e12fff1e 	bx	lr

000105b4 <main>:

void main()
{
   105b4:	e92d4800 	push	{fp, lr}
   105b8:	e28db004 	add	fp, sp, #4
   105bc:	e24dd070 	sub	sp, sp, #112	; 0x70
    char s0[] = "Hello, asm!";
   105c0:	e59f2064 	ldr	r2, [pc, #100]	; 1062c <main+0x78>
   105c4:	e24b3010 	sub	r3, fp, #16
   105c8:	e8920007 	ldm	r2, {r0, r1, r2}
   105cc:	e8830007 	stm	r3, {r0, r1, r2}
    char s1[100];

    reverse(&s1[0], &s0[0]);
   105d0:	e24b2010 	sub	r2, fp, #16  ; 16
   105d4:	e24b3074 	sub	r3, fp, #116	; 0x74 116-16 = 100
   105d8:	e1a01002 	mov	r1, r2          ; r1 = fp-16 = s0[0]
   105dc:	e1a00003 	mov	r0, r3          ; r0 = fp-116 = s1[0]
   105e0:	ebffffcd 	bl	1051c <reverse>
    printf("%s\n", &s1[0]);
   105e4:	e24b3074 	sub	r3, fp, #116	; 0x74 s1[0]
   105e8:	e1a00003 	mov	r0, r3
   105ec:	ebffff6a 	bl	1039c <puts@plt>

    reverse2(&s1[0]);
   105f0:	e24b3074 	sub	r3, fp, #116	; 0x74 s1[0]
   105f4:	e1a00003 	mov	r0, r3
   105f8:	ebffffd1 	bl	10544 <reverse2>
    printf("%s\n", &s1[0]);
   105fc:	e24b3074 	sub	r3, fp, #116	; 0x74 s1[0]
   10600:	e1a00003 	mov	r0, r3
   10604:	ebffff64 	bl	1039c <puts@plt>

    printf("%x\n", sum(0x666, 0x777));
   10608:	e59f1020 	ldr	r1, [pc, #32]	; 10630 <main+0x7c>
   1060c:	e59f0020 	ldr	r0, [pc, #32]	; 10634 <main+0x80>
   10610:	ebffffd9 	bl	1057c <sum>
   10614:	e1a03000 	mov	r3, r0
   10618:	e1a01003 	mov	r1, r3
   1061c:	e59f0014 	ldr	r0, [pc, #20]	; 10638 <main+0x84>
   10620:	ebffff57 	bl	10384 <printf@plt>
}


/*
 * arm-v8
 */
void reverse(char * dst, const char * src)
{
  400630:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400634:	910003fd 	mov	x29, sp
  400638:	f9000fa0 	str	x0, [x29, #24] ; dst
  40063c:	f9000ba1 	str	x1, [x29, #16] ; src
    strcpy(dst, src);
  400640:	f9400ba1 	ldr	x1, [x29, #16] ; src
  400644:	f9400fa0 	ldr	x0, [x29, #24] ; dst
  400648:	97ffff9e 	bl	4004c0 <strcpy@plt>
}
  40064c:	d503201f 	nop
  400650:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400654:	d65f03c0 	ret

0000000000400658 <reverse2>:

void reverse2(char *s)
{
  400658:	d10043ff 	sub	sp, sp, #0x10
  40065c:	f90007e0 	str	x0, [sp, #8] ; s
    strcpy(s, "hello reverse.");
  400660:	f94007e2 	ldr	x2, [sp, #8] ; s
  400664:	90000000 	adrp	x0, 400000 <_init-0x440>
  400668:	911f0001 	add	x1, x0, #0x7c0 ; string
  40066c:	aa0203e0 	mov	x0, x2 ; s
  400670:	f9400022 	ldr	x2, [x1] ; string ---> s
  400674:	f9000002 	str	x2, [x0] ; s ---> s
  400678:	f8407021 	ldur	x1, [x1, #7] ; 非对齐偏移
  40067c:	f8007001 	stur	x1, [x0, #7] ; 非对齐偏移 s
}
  400680:	d503201f 	nop
  400684:	910043ff 	add	sp, sp, #0x10
  400688:	d65f03c0 	ret

000000000040068c <sum>:

int sum(int a, int b)
{
  40068c:	d10043ff 	sub	sp, sp, #0x10
  400690:	b9000fe0 	str	w0, [sp, #12] ; a
  400694:	b9000be1 	str	w1, [sp, #8]  ; b
    a += b;
  400698:	b9400fe1 	ldr	w1, [sp, #12] ; a
  40069c:	b9400be0 	ldr	w0, [sp, #8]  ; b
  4006a0:	0b000020 	add	w0, w1, w0    ; a + b
  4006a4:	b9000fe0 	str	w0, [sp, #12] ; a = a + b
    return a;
  4006a8:	b9400fe0 	ldr	w0, [sp, #12]
}
  4006ac:	910043ff 	add	sp, sp, #0x10
  4006b0:	d65f03c0 	ret

00000000004006b4 <main>:

void main()
{
  4006b4:	a9b77bfd 	stp	x29, x30, [sp, #-144]!
  4006b8:	910003fd 	mov	x29, sp
    char s0[] = "Hello, asm!";
  4006bc:	90000000 	adrp	x0, 400000 <_init-0x440>
  4006c0:	911f6001 	add	x1, x0, #0x7d8 ; string
  4006c4:	910203a0 	add	x0, x29, #0x80 ; s0
  4006c8:	f9400022 	ldr	x2, [x1]       ; string ---> x2
  4006cc:	f9000002 	str	x2, [x0]       ; x2 ---> s0
  4006d0:	b9400821 	ldr	w1, [x1, #8]   ; load next 8 char
  4006d4:	b9000801 	str	w1, [x0, #8]   ; save next 8 char
    char s1[100];

    reverse(&s1[0], &s0[0]);
  4006d8:	910203a1 	add	x1, x29, #0x80 ; s0
  4006dc:	910063a0 	add	x0, x29, #0x18 ; s1
  4006e0:	97ffffd4 	bl	400630 <reverse>
    printf("%s\n", &s1[0]);
  4006e4:	910063a0 	add	x0, x29, #0x18 ; s1
  4006e8:	97ffff72 	bl	4004b0 <puts@plt>

    reverse2(&s1[0]);
  4006ec:	910063a0 	add	x0, x29, #0x18 ; s1
  4006f0:	97ffffda 	bl	400658 <reverse2>
    printf("%s\n", &s1[0]);
  4006f4:	910063a0 	add	x0, x29, #0x18 ; s1
  4006f8:	97ffff6e 	bl	4004b0 <puts@plt>

    printf("%x\n", sum(0x666, 0x777));
  4006fc:	5280eee1 	mov	w1, #0x777                 	// #1911
  400700:	5280ccc0 	mov	w0, #0x666                 	// #1638
  400704:	97ffffe2 	bl	40068c <sum>
  400708:	2a0003e1 	mov	w1, w0
  40070c:	90000000 	adrp	x0, 400000 <_init-0x440>
  400710:	911f4000 	add	x0, x0, #0x7d0
  400714:	97ffff6f 	bl	4004d0 <printf@plt>
}
#endif
